package cn.ulyer.core.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ulyer.common.jpa.entity.ActionLog;
import cn.ulyer.common.jpa.service.AbstractService;
import cn.ulyer.core.modules.system.service.TActionLogService;
import cn.ulyer.core.modules.system.vo.ActionLogVo;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.stereotype.Service;
import cn.ulyer.common.jpa.entity.QActionLog;

@Service
public class TActionLogServiceImpl extends AbstractService<ActionLog, Integer> implements TActionLogService {

    @Override
    protected JPAQuery<ActionLog> buildPageQuery(Object queryModel) {
        //先编译Q类再打开注释
        ActionLogVo entity = (ActionLogVo) queryModel;
        QActionLog qEntity = QActionLog.actionLog;
        JPAQuery<ActionLog> typeQuery = jpaQueryFactory.selectFrom(qEntity);
        if (entity != null) {
            if (StrUtil.isNotBlank(entity.getAction())) {
                typeQuery.where(qEntity.action.eq(entity.getAction()));
            }
            if (entity.getActionUserId() != null) {
                typeQuery.where(qEntity.actionUserId.eq(entity.getActionUserId()));
            }
            if (StrUtil.isNotBlank(entity.getHttpMethod())) {
                typeQuery.where(qEntity.httpMethod.eq(entity.getHttpMethod()));
            }
            if (StrUtil.isNotBlank(entity.getJavaMethod())) {
                typeQuery.where(qEntity.javaMethod.eq(entity.getJavaMethod()));
            }
            if (StrUtil.isNotBlank(entity.getModule()) ) {
                typeQuery.where(qEntity.module.eq(entity.getModule()));
            }
            if (StrUtil.isNotBlank(entity.getRequestIp())) {
                typeQuery.where(qEntity.requestIp.eq(entity.getRequestIp()));
            }
            if (StrUtil.isNotBlank(entity.getRequestUrl()) ) {
                typeQuery.where(qEntity.requestUrl.eq(entity.getRequestUrl()));
            }
            if (entity.getBeginTime() != null) {
                typeQuery.where(qEntity.createTime.gt(entity.getBeginTime()));
            }
            if(entity.getEndTime()!=null){
                typeQuery.where(qEntity.createTime.lt(entity.getEndTime()));
            }
        }
        typeQuery.orderBy(qEntity.createTime.desc());
        return typeQuery;
    }

}
